import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询备件PI'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询备件PI详情'
});
const newOrderDesc = formatMessage({
    id: 'api.message.newOrderDesc',
    defaultMessage: '新增备件PI'
});
const modifyDesc = formatMessage({
    id: 'api.message.modifyDesc',
    defaultMessage: '修改备件PI'
});
const approveDesc = formatMessage({
    id: 'api.message.approveDesc',
    defaultMessage: '备件PI审核'
});
const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '备件PI作废'
});
const cancelDesc = formatMessage({
    id: 'api.message.cancelDesc',
    defaultMessage: '备件PI取消'
});
const confirmDesc = formatMessage({
    id: 'api.message.confirmDesc',
    defaultMessage: '确认备件PI'
});
const getDealersDesc = formatMessage({
    id: 'api.message.getDealersDesc',
    defaultMessage: '查询总代信息'
});
const getAccountBalanceDesc = formatMessage({
    id: 'api.message.getAccountBalanceDesc',
    defaultMessage: '备件资金账户查询'
});
const getPartSaleOrderDesc = formatMessage({
    id: 'api.message.getPartSaleOrderDesc',
    defaultMessage: '备件销售订单查询'
});
const getPartSalePriceDesc = formatMessage({
    id: 'api.message.getPartSalePriceDesc',
    defaultMessage: '备件销售价格查询'
});

const getHistoryListDesc = formatMessage({
    id: 'api.getHistoryListDesc',
    defaultMessage: '查询备件需求计划历史'
});

const getHistoryDetailDesc = formatMessage({
    id: 'api.getHistoryDetailDesc',
    defaultMessage: '查询备件需求计划历史详情'
});

const getPiExcelDesc = formatMessage({
    id: 'api.message.getPiExcelDesc',
    defaultMessage: '备件Pi下载Excel'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/partOrderPi/init`, null, dto.init)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
// 查询备件pi列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partOrderPis/forCurrentUser?${queryString}`, null, dto.list)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/partOrderPis/${id}`, null, dto.detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//新增
export const addData = data => request(`${baseUrl}/partOrderPis`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newOrderDesc));
//修改
export const modifyData = (id, data) => request(`${baseUrl}/partOrderPis/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDesc));
//确认
export const confirmData = (id, data) => request(`${baseUrl}/partOrderPis/${id}/confirm`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, confirmDesc));
//审核
export const auditData = (id, data) => request(`${baseUrl}/partOrderPis/${id}/audit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));
//作废
export const abandonData = (id, data) => request(`${baseUrl}/partOrderPis/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));
//取消
export const cancelData = (id, data) => request(`${baseUrl}/partOrderPis/${id}/cancel`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, cancelDesc));
// 快捷查询总代
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forPersonnel/simple?${queryString}`, null, dto.dealerSelectList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forPersonnel?${queryString}`, null, dto.dealerSelectList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 备件资金账户查询
export const getAccountBalance = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/accountQuerys/forCurrentUser?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAccountBalanceDesc));
};
// 销售订单列表查询
export const getPartSaleOrders = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partSaleOrders/forPartOrderPi?${queryString}`, null, dto.partSaleOrderList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartSaleOrderDesc));
};
// 根据id查询销售订单
export const getPartSaleOrderById = id =>
    request(`${baseUrl}/partSaleOrders/${id}/forPartOrderPi`, null, dto.partSaleOrderDetail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartSaleOrderDesc));
// 备件销售价格
export const getPartPrice = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partSalesPrices/forPartOrderPi?${queryString}`, null, dto.partSalePirce)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartSalePriceDesc));
};
//查询开户银行
export const getBanks = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/banks/forPartOrderPi?${queryString}`, null, dto.bank)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartSaleOrderDesc));
};
//备件PI历史查询
export const getHistoryList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/partOrderPiHistories/forCurrentUser?${queryString}`, null, dto.HistoryList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getHistoryListDesc));
};
//备件PI历史查询详情
export const getHistoryDetail = id => request(`${baseUrl}/partOrderPiHistories/${id}`, null, dto.HistoryDetail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getHistoryDetailDesc));
//备件PI历史附件下载
export const getHistoryDownload = data => {
    const queryString = stringify(data);
    return request(`${baseUrl}/files/ftpIsDownload?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, null));
};
//导出piExcel
export const getPiExcel = id => request(`${baseUrl}/partOrderPis/${id}/exportExcel`, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, getPiExcelDesc))
    .then(res => res.data);
